<template>
  <div class="">
    <div id="top"></div>
  </div>
</template>

<script lang="ts" setup>
import echarts from "echarts";
import { onMounted } from "vue";
import { getForeinData } from "../../charts";
var data = [77.7, 88.1, 92.6, 0.4, 3.3];

var valdata = [];
var myColor = ["#1089E7", "#F57474", "#56D0E3", "#F8B448", "#8B78F6"];
let option = {
  title: {
    text: "主要疫情国家治愈率",
    x: "center",
    textStyle: {
      color: "#fff",
    },
  },
  backgroundColor: "#0e2147",
  xAxis: {
    show: false,
  },
  yAxis: [
    {
      // boundaryGap:true,
      show: true,
      data: ["美国", "西班牙", "法国", "意大利", "德国"],
      inverse: true,
      axisLine: {
        show: false,
      },
      splitLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        color: "#fff",
      },
    },
    {
      show: true,
      inverse: true,
      data: valdata,
      axisLabel: {
        textStyle: {
          fontSize: 8,
          color: "#fff",
        },
      },
      axisLine: {
        show: false,
      },
      splitLine: {
        show: false,
      },
      axisTick: {
        show: false,
      },
    },
  ],
  series: [
    {
      name: "条",
      type: "bar",
      yAxisIndex: 0,
      data: data,
      barWidth: 15,
      itemStyle: {
        normal: {
          barBorderRadius: 30,
          color: function (params) {
            var num = myColor.length;
            return myColor[params.dataIndex % num];
          },
        },
      },
      label: {
        normal: {
          show: true,
          position: "inside",
          formatter: "{c}%",
        },
      },
    },
    {
      name: "框",
      type: "bar",
      yAxisIndex: 1,
      barGap: "-100%",
      data: [100, 100, 100, 100, 100],
      barWidth: 15,
      itemStyle: {
        normal: {
          color: "none",
          borderColor: "#00c1de",
          borderWidth: 3,
          barBorderRadius: 15,
        },
      },
    },
  ],
};

onMounted(() => {
  var chartDom = document.getElementById("top");
  var myChart = echarts.init(chartDom);
  getForeinData().then((res) => {
    let arr = res.foreignList.slice(0, 5);
    option.yAxis[0].data = arr.map((item) => item.name);
    // console.log(arr);

    myChart.setOption(option);
    window.addEventListener(
      "resize",
      function () {
        myChart.resize();
      },
      false
    );
  });
});
</script>

<style lang="scss">
#top {
  height: 325px;
  text-align: center;
  padding: 0 10px;
  margin-top: 10px;
}
</style>
